package com.baidu.videoseries.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.videoseries.R;
import com.baidu.videoseries.bean.MediaBean;
import com.kvin.toolkit.utils.StringUtils;

import java.util.ArrayList;

/**
 * Created by sunmo on 2016/12/11.
 */
public class AudioAdapter extends RecyclerView.Adapter<AudioAdapter.ViewHolder> {
    private ArrayList<MediaBean> data;
    private Context context;

    private OnItemClickListener onItemClickListener;

    public AudioAdapter(ArrayList<MediaBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public AudioAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.adapter_audio, parent, false);
        return new AudioAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(AudioAdapter.ViewHolder vh, int position) {
        MediaBean bean = data.get(position);
        vh.nameTv.setText(StringUtils.getString(bean.getStoryName()));
        vh.authorTv.setText(StringUtils.getString(bean.getAuthor()));
        vh.durationTv.setText(StringUtils.getString(bean.getDuration()));
        vh.itemContainer.setTag(position);
        vh.itemContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null)
                    onItemClickListener.onItemClick((Integer) v.getTag());
            }
        });
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    /**
     * view holder
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout itemContainer;
        TextView nameTv;
        TextView authorTv;
        TextView durationTv;

        public ViewHolder(View v) {
            super(v);
            this.itemContainer = (LinearLayout) v.findViewById(R.id.item_container);
            this.nameTv = (TextView) v.findViewById(R.id.name_tv);
            this.authorTv = (TextView) v.findViewById(R.id.author_tv);
            this.durationTv = (TextView) v.findViewById(R.id.duration_tv);
        }
    }
}
